<?php


namespace app\erp\controller;


use app\erp\model\Buildtpldb;
use app\erp\model\Dianzidb;
use app\erp\model\Orderdb;
use app\erp\model\UBuildtpldb;
use app\erp\model\UOrderdb;
use think\Db;

class Dianzi extends Base
{
    protected $cols = array(
        array(
            'field' => 'id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'order_no',
            'width' => 160,
            'title' => '订单号'
        ),
        array(
            'field' => 'out_order_no',
            'width' => 160,
            'title' => '军网订单号'
        ),
        array(
            'field' => 'order_money',
            'width' => 120,
            'title' => '订单金额'
        ),
        array(
            'field' => 'apply_time',
            'width' => 160,
            'title' => '申请时间'
        ),
        array(
            'field' => 'company_name',
            'width' => 160,
            'title' => '申请供应商',
            'templet'=>'#companyNameTpl'
        ),
        array(
            'field' => 'weishoukuan',
            'width' => 160,
            'title' => '供应商未收款'
        ),
        array(
            'field' => 'remark',
            'width' => 160,
            'title' => '备注'
        ),
        array(
            'field' => 'apply_state',
            'width' => 120,
            'title' => '审核状态'
        ),
        array(
            'field' => 'username',
            'width' => 160,
            'title' => '审核人'
        ),
        array(
            'field' => 'audit_time',
            'width' => 160,
            'title' => '审核时间'
        ),
        array(
            'field' => 'apply_state',
            'width' => 120,
            'title' => '审核状态',
            'templet' => '#dianzi_apply_stateTpl'
        ),
        array(
            'field' => 'reason',
            'width' => 160,
            'title' => '驳回原因',
        ),
        array(
            'fixed' => 'right',
            'width' => 160,
            'toolbar' => '#dianzi_tool',
            'title' => '操作'
        )
    );
    protected $cols_history = array(
        array(
            'field' => 'out_order_no',
            'width' => 200,
            'title' => '军网订单号'
        ),
        array(
            'field' => 'deff_time',
            'width' => 120,
            'title' => '回款天数'
        )
    );
    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $this->post = $request->param();
    }

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function ajaxList()
    {
        $dianzidb = new Dianzidb();
        $dianzidb->param = $this->post;
        $result = $dianzidb
            ->with(['supplier', 'auditer'])->order('id desc')
            ->paginate($this->post['limit']);
        $this->layuiReturn($result->items(), $result->total());
    }

    /**
     * title("驳回审核")
     */
    public function ajaxRefuse()
    {
        $dianzidb = new Dianzidb();
        $get_dianzi = $dianzidb->find($this->post['id']);
        if ($get_dianzi) {
            $save_dianzi = [];
            $save_dianzi['apply_state'] = 2;
            $save_dianzi['audit_member_id'] = MEMBER_ID;
            $save_dianzi['audit_time'] = date('Y-m-d H:i:s');
            $save_dianzi['reason'] = $this->post['reason'];
            $get_dianzi->save($save_dianzi);

            // 将分系统下的状态修改
            $uorderdb = new UOrderdb();
            $get_order = $uorderdb->where('order_no', $get_dianzi['order_no'])->find();
            if ($get_order) {
                $get_order->save(['is_dianzi' => 0]);
            }
            $this->layuiReturn('success');
        } else {
            $this->layuiReturn('error', 0, '错误', -1);
        }
    }

    /**
     * Title("审核通过")
     */
    public function ajaxPass()
    {
        $dianzidb = new Dianzidb();
        $get_dianzi = $dianzidb->find($this->post['id']);
        if ($get_dianzi) {
            $save_dianzi = [];
            $save_dianzi['apply_state'] = 1;
            $save_dianzi['audit_member_id'] = MEMBER_ID;
            $save_dianzi['audit_time'] = date('Y-m-d H:i:s');
            $get_dianzi->save($save_dianzi);

            // 修改主系统订单
                $orderdb = new Orderdb();
                $get_order = $orderdb->where('order_no', $get_dianzi['order_no'])->find();
                if ($get_order) {
                    $save_order = [];
                    $save_order['is_dianzi'] = 1;// 设置状态
                    $save_order['cght'] = '';//重新生成采购合同
                    $save_order['supplier_id'] = 10602;// 审批之后1级供应商变成10602
                    $save_order['supplier_rate'] = 97;// 审批之后1级供应商折扣率变成97
                    $get_order->save($save_order);
                    // 添加合同生成记录
                    $buildtpldb = new Buildtpldb();
                    $save_build = [];
                    $save_build['buildtpl_type'] = 'cght';
                    $save_build['order_no'] = $get_order['order_no'];
                    $save_build['state'] = 2;
                    $save_build['create_time'] = time();
                    $buildtpldb->save($save_build);
                }

            // 修改分系统订单
            $uorderdb = new UOrderdb();
            $get_uorder = $uorderdb->where('order_no', $get_dianzi['order_no'])->find();
            if ($get_uorder) {
                $save_uorder = [];
                $save_uorder['cght'] = '';
                $save_uorder['fwht'] = '';
                $get_uorder->save($save_uorder);
                // 添加合同生成记录
                $save_ubuild = [];
                $temp = [];
                $temp['buildtpl_type'] = 'cght';
                $temp['order_no'] = $get_order['order_no'];
                $temp['state'] = 2;
                $temp['create_time'] = time();
                array_push($save_ubuild, $temp);
                $temp = [];
                $temp['buildtpl_type'] = 'fwht';
                $temp['order_no'] = $get_order['order_no'];
                $temp['state'] = 2;
                $temp['create_time'] = time();
                array_push($save_ubuild, $temp);
                $ubuildtpldb = new UBuildtpldb();
                $ubuildtpldb->saveAll($save_ubuild);
            }


            $this->layuiReturn('success');
        } else {
            $this->layuiReturn('error', 0, '错误', -1);
        }
    }

    /**
     * Title("历史回款时长")
     */
    public function history()
    {
        $this->assign('cols', json_encode($this->cols_history));
        $this->assign('phone',$this->post['phone']);
        return $this->fetch();
    }

    public function historyList(){
        $get_order = Db::name('order')
            //->where('phone','19187084023')
            ->where('phone', $this->post['phone'])
            ->where('pay_state',2)
            ->field('out_order_no,CEIL((pay_time-create_time)/86400) as deff_time')
            ->order('order_id desc')
            ->paginate($this->post['limit']);
        $this->layuiReturn($get_order->items(), $get_order->total());
    }


}